package com.hgxd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hgxd.annotation.OperationLogAnnotation;
import com.hgxd.entity.vo.pagevo.EquipmentMaintenanceRecordsPageVo;
import com.hgxd.result.Result;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiOperation;

import javax.annotation.Resource;
import java.util.List;

import com.hgxd.service.IEquipmentMaintenanceRecordsService;
import com.hgxd.entity.pojo.EquipmentMaintenanceRecords;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 设备维护记录表 前端控制器
 * </p>
 *
 * @author wtf
 * @since 2023-11-08
 */
@Api(tags = "设备维护记录管理")
@RestController
@RequestMapping("/api/equipmentMaintenanceRecords")
@CrossOrigin
public class EquipmentMaintenanceRecordsController {
    @Resource(name = "equipmentMaintenanceRecordsServiceImpl")
    private IEquipmentMaintenanceRecordsService equipmentMaintenanceRecordsService;

    /**
     * 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增设备记录", notes = "无需传入id")
    @OperationLogAnnotation(operModule = "设备维护记录管理-新增", operType = "新增", operContent = "新增设备维护记录")
    public Result<Boolean> insert(@RequestBody EquipmentMaintenanceRecords equipmentMaintenanceRecords) {
        return Result.ok(equipmentMaintenanceRecordsService.save(equipmentMaintenanceRecords));
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改设备记录", notes = "必须传入id")
    @OperationLogAnnotation(operModule = "设备维护记录管理-修改", operType = "修改", operContent = "修改设备维护记录")
    public Result<Boolean> update(@RequestBody EquipmentMaintenanceRecords equipmentMaintenanceRecords) {
        return Result.ok(equipmentMaintenanceRecordsService.updateById(equipmentMaintenanceRecords));
    }

    /**
     * 根据id删除
     */
    @PostMapping("/delete/{id}")
    @ApiOperation(value = "根据id删除设备维护记录", notes = "必传id")
    @OperationLogAnnotation(operModule = "设备维护记录管理-根据id删除", operType = "删除", operContent = "根据id删除设备维护记录")
    public Result<Boolean> delete(@PathVariable Integer id) {
        return Result.ok(equipmentMaintenanceRecordsService.removeById(id));
    }

    /**
     * 批量删除
     */
    @PostMapping("/del/batch")
    @ApiOperation(value = "批量删除", notes = "传入ids")
    @OperationLogAnnotation(operModule = "设备维护记录管理-批量删除", operType = "删除", operContent = "批量删除设备维护记录")
    public Result<Boolean> deleteBatch(@RequestBody List<Integer> ids) {
        return Result.ok(equipmentMaintenanceRecordsService.removeByIds(ids));
    }

    /**
     * 查询全部
     */
    @GetMapping("/getAll")
    @ApiOperation(value = "查询全部维护设备记录", notes = "无需传入参数")
    @OperationLogAnnotation(operModule = "设备维护记录管理-查询全部维护设备记录", operType = "查询", operContent = "查询全部设备维护记录")
    public Result<List<EquipmentMaintenanceRecords>> findAll() {
        return Result.ok(equipmentMaintenanceRecordsService.list());
    }

    /**
     * 根据id查询一个
     */
    @GetMapping("getById/{id}")
    @ApiOperation(value = "根据id查询设备维护记录", notes = "必传id")
    @OperationLogAnnotation(operModule = "设备维护记录管理-根据id查询一条设备维护管理", operType = "查询", operContent = "根据id查询一条设备维护记录")
    public Result<EquipmentMaintenanceRecords> findOne(@PathVariable Integer id) {
        return Result.ok(equipmentMaintenanceRecordsService.getById(id));
    }

    /**
     * 分页模糊查询
     */
    @PostMapping("/get/page")
    @ApiOperation(value = "分页模糊查询", notes = "必传pageNum和pageSize")
    public Result<Page<EquipmentMaintenanceRecords>> findPage(@RequestBody EquipmentMaintenanceRecordsPageVo equipmentMaintenanceRecordsPageVo) {
        LambdaQueryWrapper<EquipmentMaintenanceRecords> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(!"".equals(equipmentMaintenanceRecordsPageVo.getTaskName()), EquipmentMaintenanceRecords::getTaskName, equipmentMaintenanceRecordsPageVo.getTaskName())
                .between(equipmentMaintenanceRecordsPageVo.getBeginTime() != null && equipmentMaintenanceRecordsPageVo.getEndingTime() != null
                        , EquipmentMaintenanceRecords::getDate, equipmentMaintenanceRecordsPageVo.getBeginTime(), equipmentMaintenanceRecordsPageVo.getEndingTime())
                .orderByDesc(EquipmentMaintenanceRecords::getId)
                .eq(!"".equals(equipmentMaintenanceRecordsPageVo.getMaintenanceResult())
                        , EquipmentMaintenanceRecords::getMaintenanceResult
                        , equipmentMaintenanceRecordsPageVo.getMaintenanceResult());

        return Result.ok(equipmentMaintenanceRecordsService.page(new Page<>(equipmentMaintenanceRecordsPageVo.getPageNum(), equipmentMaintenanceRecordsPageVo.getPageSize()), queryWrapper));
    }
}

